/*
 * @Date: 2023-01-22 02:17:37
 * @LastEditors: 恶名扬 2080156129@qq.com
 * @LastEditTime: 2023-01-24 02:11:28
 * @FilePath: \campus-repair-platform\server\service\manage\feedback.js
 */
/**
 * getFeedbackList  获取意见反馈列表
 * queryFeedback    根据title查询意见反馈
 * getFeedbackInfo  获取意见反馈信息
 */

const feedbackStatement = require("../../common/sql_sentences/feedback")

exports.getFeedbackList = async (req, res) => {
  try {
    const { type, page, pagesize } = req.query
    // 获取反馈总数
    let total,
      sqlStr = feedbackStatement.selectCount({ type })
    let results = await RunSQL(sqlStr)
    if (!results[0].total) return res.successSend("没有找到反馈信息！", 10005)
    total = results[0].total

    // 获取反馈列表
    sqlStr = feedbackStatement.selectLimit({ type }, page, pagesize)
    results = await RunSQL(sqlStr)
    return res.successSend("", 10000, { row: results, total })
  } catch (error) {
    res.errSend(error)
  }
}

exports.queryFeedback = async (req, res) => {
  try {
    const { title, page, pagesize, type } = req.query
    // 查找意见反馈
    let total, sqlStr = feedbackStatement.selectCountLike({ type }, title)
    console.log(sqlStr)
    let results = await RunSQL(sqlStr)
    if (results.length !== 1) return res.successSend("没有找到反馈信息！", 10005)
    total = results[0].total

    // 根据title模糊查询获取意见反馈列表
    sqlStr = feedbackStatement.selectLikeLimit({ type }, title, page, pagesize)
    results = await RunSQL(sqlStr)
    return res.successSend("", 10000, { row: results, total })
  } catch (error) {
    res.errSend(error)
  }
}

exports.getFeedbackInfo = async (req, res) => {
  try {
    // 获取反馈信息
    const sqlStr = feedbackStatement.idSelect(req.params.id)
    let results = await RunSQL(sqlStr)
    return res.successSend("", 10000, results[0])
  } catch (error) {
    res.errSend(error)
  }
}
